package com.znjc.web.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.znjc.common.annotation.Log;
import com.znjc.common.core.controller.BaseController;
import com.znjc.common.core.domain.AjaxResult;
import com.znjc.common.core.domain.entity.SysDept;
import com.znjc.common.core.page.TableDataInfo;
import com.znjc.common.enums.BusinessType;
import com.znjc.common.utils.StringUtils;
import com.znjc.common.utils.poi.ExcelUtil;
import com.znjc.device.domain.DeviceCommand;
import com.znjc.device.domain.DeviceDevice;
import com.znjc.device.domain.DeviceType;
import com.znjc.device.service.IDeviceCommandService;
import com.znjc.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 【命令】Controller
 *
 * @author zengpc
 * @date 2023-12-04
 */
@RestController
@RequestMapping("/device/command")
public class DeviceCommandController extends BaseController {
    @Autowired
    private IDeviceCommandService deviceCommandService;
    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询命令管理列表
     */
    @PreAuthorize("@ss.hasPermi('device:command:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceCommand deviceCommand) {
        startPage();
        LambdaQueryWrapper<DeviceCommand> wrapper = getWrapper(deviceCommand);
        List<DeviceCommand> list = deviceCommandService.list(wrapper);
        return getDataTable(packList(list));
    }

    /**
     * 导出命令管理列表
     */
    @PreAuthorize("@ss.hasPermi('device:command:export')")
    @Log(title = "命令管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceCommand deviceCommand) {
        LambdaQueryWrapper<DeviceCommand> wrapper = getWrapper(deviceCommand);
        List<DeviceCommand> list = deviceCommandService.list(wrapper);
        ExcelUtil<DeviceCommand> util = new ExcelUtil<DeviceCommand>(DeviceCommand.class);
        util.exportExcel(response, packList(list), "命令管理数据");
    }

    /**
     * 获取命令管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:command:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(deviceCommandService.getById(id));
    }

    /**
     * 新增命令管理
     */
    @PreAuthorize("@ss.hasPermi('device:command:add')")
    @Log(title = "命令管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceCommand deviceCommand) {
        return toAjax(deviceCommandService.save(deviceCommand));
    }

    /**
     * 修改命令管理
     */
    @PreAuthorize("@ss.hasPermi('device:command:edit')")
    @Log(title = "命令管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceCommand deviceCommand) {
        return toAjax(deviceCommandService.updateById(deviceCommand));
    }

    /**
     * 删除命令管理
     */
    @PreAuthorize("@ss.hasPermi('device:command:remove')")
    @Log(title = "命令管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(deviceCommandService.removeByIds(new ArrayList<>(Arrays.asList(ids))));
    }
    /**
     * 查询公共方法
     *
     * @param entity
     * @return
     */
    private LambdaQueryWrapper getWrapper(DeviceCommand entity) {
        LambdaQueryWrapper<DeviceCommand> wrapper = new LambdaQueryWrapper<>();
        if (null != entity) {
            if (StringUtils.isNotEmpty(entity.getName())) {
                wrapper.like(DeviceCommand::getName, entity.getName());
            }
            if (StringUtils.isNotEmpty(entity.getCommandType())) {
                wrapper.eq(DeviceCommand::getCommandType, entity.getCommandType());
            }
        }
        return wrapper;
    }

    /**
     * 结果封装
     *
     * @param list
     * @return
     */
    private List<DeviceCommand> packList(List<DeviceCommand> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(entity -> {
                if (null != entity.getDeptId()) {
                    SysDept dept = deptService.selectDeptById(entity.getDeptId());
                    if (null != dept) {
                        entity.setDeptName(dept.getDeptName());
                    }
                }
            });
        }
        return list;
    }
}
